/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2020年5月12日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.studyjdk.thread;

/**
 * @Type DeadLock
 * @Desc
 * @author 52489
 * @date 2020年5月12日
 * @Version V1.0
 */
public class DeadLock {
    public static void main(String[] args) {

        MakeUp girl1 = new MakeUp(1, "灰姑娘");
        MakeUp girl2 = new MakeUp(0, "白雪公主");
        girl1.start();
        girl2.start();
    }

}

class Mirror {

}

class LispStick {

}

class MakeUp extends Thread {
    public static void main(String[] args) {
        System.out.println(111111);
    }

    static Mirror mirror = new Mirror();
    static LispStick lispStick = new LispStick();
    int choice;

    String girlName;

    /**
     * @param choice
     * @param girlName
     */
    public MakeUp(int choice, String girlName) {
        this.choice = choice;
        this.girlName = girlName;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Thread#run()
     */
    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
        // 获得 口红锁
        if (choice == 0) {
            synchronized (lispStick) {
                System.out.println(this.girlName + "获得口红的锁");
                Thread.sleep(1000);
                synchronized (mirror) {
                    System.out.println(this.girlName + "获得镜子的锁");
                }
            }
        } else {
            synchronized (mirror) {
                System.out.println(this.girlName + "获得镜子的锁");
                Thread.sleep(2000);
                synchronized (lispStick) {
                    System.out.println(this.girlName + "获得口红的锁");
                }
            }
        }
    }
}
